# -*- coding: utf-8 -*-

import datetime
from sqlalchemy import Column, MetaData, Table, types, ForeignKey
from xadmin.model import tableprefix, metadata, Item

risks_table = Table(tableprefix+"risks", metadata,
    Column("id", types.Integer, primary_key=True, autoincrement=True),
    Column("name", types.String(255), nullable=False),
    Column("description", types.TEXT),
    Column("probability", types.Float, default=0),
    Column("impact", types.Float, default=0),
    Column("created", types.DateTime, default=datetime.datetime.now()),
    Column("updated", types.DateTime, onupdate=datetime.datetime.now())
    )

class Risk(Item):
    def __init__(self, name=None, description=None, probability=None, impact=None):
        self.name = name
        self.description = description
        self.probability = probability
        self.impact = impact
        self.mapping = { 'name':  {'map': '/risk/name/text()', 'obligatory': True}
                        , 'description': {'map': '/risk/description/text()', 'obligatory': True}
                        , 'probability': {'map': '/risk/probability/text()'}
                        , 'impact': {'map': '/risk/impact/text()'}
                        }

    def __repr__(self):
        return "Risk('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (self.id, self.name, self.description, self.probability, self.impact, self.created, self.updated)